package com.lp.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.lp.common.Code;
import com.lp.dao.BaseDao;
import com.lp.service.BaseService;
import com.lp.util.ObjectUtil;
import com.lp.util.PageBean;
import com.lp.util.ResultMapUtils;

@Component("baseService")
public class BaseServiceImpl extends ResultMapUtils implements BaseService {

	@Autowired
	@Qualifier("baseDao")
	protected BaseDao dao;
	
	/**
	 * 异常处理
	 * @param e
	 * @param resultMap
	 */
	protected void exception(Exception e, Map<String, Object> resultMap, Object... param) {
		putStatusCode(resultMap, Code.ResponseCode.SystemCode.ERROR);
		exception(e, param);
	}
	
	@Override
	public Map<String, Object> selectList(String sqlMapperKey, Object param, Object... params) {
		Map<String, Object> resultMap = getResultMap();
		try {
			List<?> data = dao.selectList(sqlMapperKey, param);
			if (data==null || data.size() ==0) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.NO_DATA);
			} else {
				putData(resultMap, data);
			}
		} catch (Exception e) {
			exception(e, resultMap, param);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPageList(String sqlMapperKey, PageBean pageBean, Object param, Object... params) {
		Map<String, Object> resultMap = getResultMap();
		try {
			int totalCount = dao.selectCount(sqlMapperKey + "Count", param);
			if (totalCount > 0) {
				List<?> list = dao.selectList(sqlMapperKey, param, pageBean);
				if(ObjectUtil.isNotEmpty(list)){
					pageBean.setTotalCount(totalCount);
					pageBean.setData(list);
					putData(resultMap, pageBean);					
				}else{
					putStatusCode(resultMap, Code.ResponseCode.SystemCode.NO_DATA);
				}
			} else {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.NO_DATA);
			}
		} catch (Exception e) {
			exception(e, resultMap, param);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectOne(String sqlMapperKey, Object param, Object... params) {
		Map<String, Object> resultMap = getResultMap();
		try {
			Object data = dao.selectOne(sqlMapperKey, param);
			if (data == null) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.NO_DATA);
			} else {
				putData(resultMap, data);
			}
		} catch (Exception e) {
			exception(e, resultMap, param);
		}
		return resultMap;
	}

	@Override
	public int count(String sqlMapperKey, Object param, Object... params) {
		int result = -1;
		try {
			result = dao.selectCount(sqlMapperKey, param);
		} catch (Exception e) {
			exception(e, param);
		}
		return result;
	}

	

	@Override
	public Map<String, Object> insert(String sqlMapperKey, Object param, Object... params) {
		Map<String, Object> resultMap = getResultMap();
		try {
			int rows = dao.insert(sqlMapperKey, param);
			if (rows <= 0)  {
				// 执行失败
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.EXEC_FAIL);
			}
		} catch (Exception e) {
			exception(e, resultMap, param);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> update(String sqlMapperKey, Object param, Object... params) {
		Map<String, Object> resultMap = getResultMap();
		try {
			int rows = dao.update(sqlMapperKey, param);
			if (rows <= 0)  {
				// 执行失败
				return putStatusCode(resultMap, Code.ResponseCode.SystemCode.EXEC_FAIL);
			}
		} catch (Exception e) {
			exception(e, resultMap, param);
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> delete(String deleteSqlMapperKey, Object param, Object... params) {
		Map<String, Object> resultMap = getResultMap();
		try {
			int rows = dao.delete(deleteSqlMapperKey, param);
			if (rows == 0) {
				return putStatusCode(resultMap, Code.ResponseCode.SystemCode.EXEC_FAIL);
			}
		} catch (Exception e) {
			exception(e, resultMap, param);
		}
		return resultMap;
	}
	
}
